<?php

namespace app\controllers\home;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\home\User;



class UserController extends Controller
{

    public $layout = false;

    /**
     * Displays about page.
     *
     * @return string
     */

    //登陆页
    public function actionLogin()
    {
        $session = Yii::$app->session;
        $session->open();
        if (!isset($session['id'])) {
            return $this->render('login');
        } else {
            return $this->redirect('daily'); 
        }
    }

    //修改密码页
    public function actionPass()
    {
        $session = Yii::$app->session;
        $session->open();
        if (isset($session['id'])) {
            return $this->render('pass');
        } else {
            return $this->redirect('login'); 
        }
    }

    //修改密码
    public function actionUpdate()
    {
        $session = Yii::$app->session;
        $session->open();
        if (isset($session['id'])) {
            $ypassword = Yii::$app->request->post('ypassword');
            $xpassword = Yii::$app->request->post('xpassword');
            $qpassword = Yii::$app->request->post('qpassword');
            if (empty($ypassword) || empty($xpassword) || empty($qpassword)) {
                $session->setFlash('demo', '请将信息填写完整');
                return $this->redirect('pass');    
            }
            if ($xpassword != $qpassword) {
                $session->setFlash('demo', '新密码两次输入不一致');
                return $this->redirect('pass');    
            }
            $row = Yii::$app->db->createCommand('SELECT password FROM user WHERE id = :id')
            ->bindValue(':id', $session['id'])
            ->queryOne();    
            if (!$row || $row['password'] != md5($ypassword) ) {
                $session->setFlash('demo', '原密码不正确');
                return $this->redirect('pass');  
            }
            if ($ypassword == $xpassword) {
                $session->setFlash('demo', '新密码不能与原密码相同');
                return $this->redirect('pass');    
            }
            $query = Yii::$app->db->createCommand('UPDATE user SET password = :password WHERE id = :id', [
                ':password' => md5($xpassword), ':id' => $session['id']
            ])->execute();
            if ($query) {
                $session->setFlash('demo', '密码修改成功请重新登录');
                $session->remove('id');
                $session->remove('name');
                $session->remove('realname');
                return $this->redirect('login');     
            }else{
                $session->setFlash('demo', '修改失败');
                return $this->redirect('pass'); 
            }    
        } else {
            return $this->redirect('login'); 
        }
    }

    //判断用户登陆
    public function actionRegister()
    {
        $session = Yii::$app->session;
        $session->open();
        $name = Yii::$app->request->post('name');
        $password = Yii::$app->request->post('password');
        if(empty($name)){
            $demo = '用户名不能为空';
        }
        if(empty($password)){
            $demo =  '密码不能为空';
        }
        $password = md5($password);
        $row = Yii::$app->db->createCommand('SELECT * FROM user WHERE name = :name')
            ->bindValue(':name', $name)
            ->queryOne();
        if ($row && $row['status'] == 2) {
            $demo = '用户被锁定，请联系管理员';
        }    
        if ($row && $row['password'] == $password) {
            $ltime = time();
            Yii::$app->db->createCommand('UPDATE user SET ltime = :ltime WHERE name = :name', [
                ':ltime' => $ltime, ':name' => $name
            ])->execute();
            $session['id'] = $row['id'];
            $session['name'] = $row['name'];
            $session['realname'] = $row['realname'];
            $demo =  true;
        } else {
            $demo =  '用户名或密码不正确';
        }
        return json_encode($demo);
    }

    //历史日报
    public function actionDaily()
    {
        $session = Yii::$app->session;
        $session->open();
        if (isset($session['id'])) {
            $id = $session['id'];
            $query = Yii::$app->db->createCommand('SELECT * FROM daily WHERE uid = :uid ORDER BY daily_date DESC')
                ->bindValue(':uid', $id)
                ->queryAll();
            $check = Yii::$app->db->createCommand('SELECT user.realname,auth.uid,auth.lid FROM user left join auth on auth.uid = user.id where lid = :lid')
                ->bindValue(':lid', $id)
                ->queryAll();    
            return $this->render('daily', ['query' => $query, 'check' => $check]);   
        } else {
            return $this->redirect('login');   
        }
             
    }

    //他人日报
    public function actionListdaily()
    {
        $session = Yii::$app->session;
        $session->open();
        if (isset($session['id'])) {
            $id = Yii::$app->request->get('id');
            $row = Yii::$app->db->createCommand('SELECT * FROM auth WHERE uid = :uid AND lid = :lid')
                ->bindValue(':uid', $id)
                ->bindValue(':lid', $session['id'])
                ->queryOne();  
            if ($row) {
                $realname = Yii::$app->request->get('realname');
                $query = Yii::$app->db->createCommand('SELECT * FROM daily WHERE uid = :uid ORDER BY daily_date DESC')
                    ->bindValue(':uid', $id)
                    ->queryAll();
                $rows = Yii::$app->db->createCommand('SELECT realname FROM user WHERE id = :id')
                    ->bindValue(':id', $id)
                    ->queryOne();    
                return $this->render('listdaily', ['query' => $query, 'realname' => $rows['realname'] ]);   
            } else {
                return $this->redirect('daily');   
            }    
        } else {
            return $this->redirect('login');   
        }
             
    }

    //注销登陆
    public function actionQuit()
    {
        $session = Yii::$app->session;
        $session->open();
        $session->remove('id');
        $session->remove('name');
        $session->remove('realname');
        return $this->redirect('login');
    }
    
}
